package online.szlib.com.rest.controller;

import java.util.List;

import online.szlib.com.common.utils.JSONHelper;
import online.szlib.com.core.domain.Catalog;
import online.szlib.com.core.services.CatalogService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping("/api/books")
public class CatalogRestController {

    private static Logger logger = LoggerFactory.getLogger(CatalogRestController.class);

    @Autowired
    private CatalogService catalogService;

	// Http Get All
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<Catalog> getAll() {
		System.out.println("List Catalogs >>>>>>>>>>>>>>>>>>>>");
		return catalogService.listBookCatalogs();
	}
	
	// Http Get One
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Catalog get(@PathVariable("id") Long id) {
		logger.debug("Get One Catalog >>>>>>>>>>>>>>>>>>>>");
		return catalogService.getBookCatalog(id);
	}
    
	// Http Create One
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Catalog> createCatalog(@RequestBody Catalog order) {
		logger.info("Create one Catalog >>>>>>>>>>>>>>>>>>>>");
		catalogService.create(order);
		return new ResponseEntity<Catalog>(order, JSONHelper.getJSONHeaders(), HttpStatus.CREATED);
	}
    
	// Http Update One Catalog
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public void update(@RequestBody Catalog entity, @PathVariable("id") Long id) {
		entity.setBookRecNo(id);
		logger.info("Update one Catalog >>>>>>>>>>>>>>>>>>>>");
		catalogService.update(entity);
	}
    
    // Http Delete One
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Long> deleteCatalog(@PathVariable Long id) {
    	logger.debug("Delete One Catalog >>>>>>>>>>>>>>>>>>>>");
    	catalogService.deleteById(id);
        return new ResponseEntity<Long>(id, HttpStatus.OK);
    }

}
